//package com.example.soapclient.service;
//
//import com.example.soapclient.model.*;
//import org.springframework.ws.client.core.WebServiceTemplate;
//import org.springframework.ws.soap.client.core.SoapActionCallback;
//import org.springframework.stereotype.Service;
//
//@Service
//public class SoapClientService {
//
//    private final WebServiceTemplate webServiceTemplate;
//
//    public SoapClientService(WebServiceTemplate webServiceTemplate) {
//        this.webServiceTemplate = webServiceTemplate;
//    }
//
//    public GetUserResponse getUserById(long userId) {
//        GetUserRequest request = new GetUserRequest();
//        request.setId(userId);
//
//        return (GetUserResponse) webServiceTemplate.marshalSendAndReceive(
//            request,
//            new SoapActionCallback("http://example.com/webservice/getUserRequest")
//        );
//    }
//
//    public GetAllUsersResponse getAllUsers() {
//        GetAllUsersRequest request = new GetAllUsersRequest();
//
//        return (GetAllUsersResponse) webServiceTemplate.marshalSendAndReceive(
//            request,
//            new SoapActionCallback("http://example.com/webservice/getAllUsersRequest")
//        );
//    }
//
//    public CreateUserResponse createUser(String name, String email, Integer age) {
//        CreateUserRequest request = new CreateUserRequest();
//        request.setName(name);
//        request.setEmail(email);
//        if (age != null) {
//            request.setAge(age);
//        }
//
//        return (CreateUserResponse) webServiceTemplate.marshalSendAndReceive(
//            request,
//            new SoapActionCallback("http://example.com/webservice/createUserRequest")
//        );
//    }
//
//    public String getRawSoapResponse(long userId) {
//        GetUserRequest request = new GetUserRequest();
//        request.setId(userId);
//
//        // 发送请求并获取原始XML响应
//        Object response = webServiceTemplate.marshalSendAndReceive(
//            request,
//            new SoapActionCallback("http://example.com/webservice/getUserRequest")
//        );
//
//        // 将响应对象转换为XML字符串
//        return convertObjectToXml(response);
//    }
//
//    private String convertObjectToXml(Object object) {
//        try {
//            javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext.newInstance(object.getClass());
//            javax.xml.bind.Marshaller marshaller = context.createMarshaller();
//            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//
//            java.io.StringWriter writer = new java.io.StringWriter();
//            marshaller.marshal(object, writer);
//
//            return writer.toString();
//        } catch (javax.xml.bind.JAXBException e) {
//            throw new RuntimeException("Failed to convert object to XML", e);
//        }
//    }
//}